package fhq.hcmute.edu.vn.gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.security.PublicKey;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;

import fhq.hcmute.edu.vn.config.ListDevicesConnected;
import fhq.hcmute.edu.vn.language.Language;
import fhq.hcmute.edu.vn.language.LanguageFactory;
import fhq.hcmute.edu.vn.network.PhoneInfos;
import fhq.hcmute.edu.vn.security.KeyManagement;

public class ConfirmDialog extends JDialog {

	private static final long serialVersionUID = -2854839703099532968L;

	private final JPanel contentPanel = new JPanel();

	public static final int ACCEPT_REMEMBER = 2;
	public static final int ACCEPT = 1;
	public static final int CANCEL = 0;
	private JLabel lblStatus;
	private JCheckBox cbRemember;
	private PhoneInfos phoneInfos;
	private int dialogResult;
	private Language language;
	private KeyManagement keyManagement;
	private PublicKey publicKey;

	public final int getDialogResult() {
		return dialogResult;
	}

	public final void setDialogResult(final int dialogResult) {
		this.dialogResult = dialogResult;
	}

	public final PhoneInfos getPhoneInfos() {
		return phoneInfos;
	}

	public final void setPhoneInfos(final PhoneInfos phoneInfos) {
		this.phoneInfos = phoneInfos;
		setMessageText(phoneInfos);
	}

	/**
	 * Create the dialog.
	 */
	public ConfirmDialog(final Frame parent, final boolean modal) {
		super(parent, modal);
		language = LanguageFactory.getInstance().getLanguage();
		this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 490, 202);
		setCenterScreen();
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		lblStatus = new JLabel("");
		lblStatus.setVerticalAlignment(SwingConstants.TOP);
		lblStatus.setHorizontalAlignment(SwingConstants.LEFT);
		lblStatus.setFont(new Font("Times New Roman", Font.PLAIN, 17));
		lblStatus.setBounds(101, 11, 363, 83);
		String sText = language.ConfirmDialogTitle();
		sText = String.format(sText, "", "");
		lblStatus.setText(sText);
		contentPanel.add(lblStatus);
		cbRemember = new JCheckBox(language.cbRemember());
		cbRemember.setFont(new Font("Times New Roman", Font.PLAIN, 17));
		cbRemember.setBounds(97, 81, 138, 23);
		contentPanel.add(cbRemember);
		JLabel lblIcon = new JLabel("");
		lblIcon.setBounds(10, 11, 81, 83);
		contentPanel.add(lblIcon);
		JPanel buttonPane = new JPanel();
		buttonPane.setBounds(10, 111, 454, 42);
		contentPanel.add(buttonPane);
		buttonPane.setLayout(null);
		JButton btnAllow = new JButton(language.btnAllow());
		btnAllow.setFont(new Font("Times New Roman", Font.PLAIN, 17));
		btnAllow.setBounds(98, 5, 100, 35);
		btnAllow.setActionCommand("OK");
		buttonPane.add(btnAllow);
		getRootPane().setDefaultButton(btnAllow);
		btnAllow.addActionListener(onClickAccept);
		JButton btnDenied = new JButton(language.btnDenied());
		btnDenied.setFont(new Font("Times New Roman", Font.PLAIN, 17));
		btnDenied.setBounds(257, 5, 100, 35);
		btnDenied.setActionCommand("Cancel");
		btnDenied.addActionListener(onClickCancel);
		buttonPane.add(btnDenied);
	}

	private void setCenterScreen() {
		int width = this.getWidth();
		int height = this.getHeight();
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		int x = (screenSize.width / 2) - (width / 2);
		int y = (screenSize.height / 2) - (height / 2);
		this.setLocation(x, y);
	}

	private ActionListener onClickAccept = new ActionListener() {

		@Override
		public void actionPerformed(final ActionEvent e) {
			dialogResult = ACCEPT;
			boolean isRemember = cbRemember.isSelected();
			dispose();
			if (isRemember) {
				try {
					ListDevicesConnected.addDataConnections(phoneInfos);
					ListDevicesConnected.commit();
					keyManagement = KeyManagement.getInstance();
					publicKey = keyManagement.generatePairKey(phoneInfos
							.getPhoneIMEI());
					keyManagement.commit();
					dialogResult = ACCEPT_REMEMBER;
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		}
	};
	private ActionListener onClickCancel = new ActionListener() {

		@Override
		public void actionPerformed(final ActionEvent e) {
			dialogResult = CANCEL;
			setVisible(false);
			dispose();
		}
	};

	public final void setMessageText(final PhoneInfos clientInfo) {
		String message = language.ConfirmDialogTitle();
		message = String.format(message, clientInfo.getDeviceName(),
				clientInfo.getDeviceModelNumber());
		lblStatus.setText(message);
	}

	public PublicKey getPublicKey() {
		return publicKey;
	}

	public void setPublicKey(PublicKey privateKey) {
		this.publicKey = privateKey;
	}
}
